package com.hpin.fansea.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.MyOrderResult;
import com.hpin.fansea.my.OrderDetailActivity;
import com.hpin.fansea.utils.Constant;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/9/21.
 */
public class MyOrderListAdapter extends BaseAdapter {

    private Context context;
    private List<MyOrderResult.OrderDetail> detailList;
    //    private Boolean isDelete = false;
    private DisplayImageOptions options;
//    // 团购实体
//    PurchaseGfdetailList purchaseDetail = null;
    // 缓存checkbox的状态
//    private static HashMap<Integer, Boolean> isSelected;

    public MyOrderListAdapter(Context context, List<MyOrderResult.OrderDetail> detailList) {
        this.context = context;
        this.detailList = detailList;
//        isSelected = new HashMap<Integer, Boolean>();

//        for (int i = 0; i < detailList.size(); i++) {
//            getIsSelected().put(i, false);
//        }

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_loading_default_list)
                .showImageForEmptyUri(R.mipmap.icon_fail_default_list)
                .showImageOnFail(R.mipmap.icon_fail_default_list)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

    public void setData(List<MyOrderResult.OrderDetail> detailList) {
        this.detailList = detailList;
    }
//
//    public void setDelete(Boolean isDelete) {
//        this.isDelete = isDelete;
//    }

    @Override
    public int getCount() {
        return detailList.size();
    }

    @Override
    public Object getItem(int position) {
        return detailList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_my_order, null);
            viewHolder = new ViewHolder();
            viewHolder.order_shopName = (TextView) convertView.findViewById(R.id.order_shopName);
            viewHolder.order_shopState = (TextView) convertView.findViewById(R.id.order_shopState);
            viewHolder.order_image = (ImageView) convertView.findViewById(R.id.order_image);
            viewHolder.order_title = (TextView) convertView.findViewById(R.id.order_title);
            viewHolder.tv_buy_time = (TextView) convertView.findViewById(R.id.tv_buy_time);
            viewHolder.order_totalPrice = (TextView) convertView.findViewById(R.id.order_totalPrice);
            viewHolder.order_totalNum = (TextView) convertView.findViewById(R.id.order_totalNum);
//            viewHolder.order_color = (TextView) convertView.findViewById(R.id.order_color);

            convertView.setTag(viewHolder);
            // viewHolder.my_purchase_checkbox = (CheckBox) convertView
            // .findViewById(R.id.my_purchase_checkbox);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // 数据填充
        initData(position, viewHolder);

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                Bundle bunder = new Bundle();
                bunder.putSerializable("orderDetail", detailList.get(position));
//                LogUtil.e("order", position + "----" + detailList.get(position).getGroupBuy().toString());
                intent.putExtras(bunder);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    private void initData(final int position, ViewHolder viewHolder) {
        // 订单时间
        viewHolder.tv_buy_time.setText(detailList.get(position).createTime2);

        MyOrderResult.OrderDetail.GroupBuy groupBuy = detailList.get(position).groupBuy;

        if (groupBuy != null) {
            // 订单号
            viewHolder.order_shopName.setText("订单号:" + detailList.get(position).ordNo);
            // shop 标题
            viewHolder.order_title.setText(groupBuy.name);
            // shop 图片
            ImageLoader.getInstance().displayImage(groupBuy.fmpica, viewHolder.order_image, options);
        }

        // shop 状态
        switch (detailList.get(position).ordStat) {

            case Constant.ORDER_STATE_NOT_PAY:
                viewHolder.order_shopState.setText("未付款");
                break;

            case Constant.ORDER_STATE_PAY:
                viewHolder.order_shopState.setText("已付款");
                break;

            case Constant.ORDER_STATE_CANEL:
                viewHolder.order_shopState.setText("已取消");
                break;

            case Constant.ORDER_STATE_VALID:
                viewHolder.order_shopState.setText("有效订单");
                break;

            case Constant.ORDER_STATE_ARRIVE:
                viewHolder.order_shopState.setText("到货订单");
                break;

            case Constant.ORDER_STATE_FINISH:
                viewHolder.order_shopState.setText("已完成");
                break;

            case Constant.ORDER_STATE_RETURN_WAIT:
                viewHolder.order_shopState.setText("退款待审核");
                break;

            case Constant.ORDER_STATE_SUBMIT_REFUND_SUCCESS:
                viewHolder.order_shopState.setText("待退款");
                break;

            case Constant.ORDER_STATE_SUBMIT_REFUND_FAIL:
                viewHolder.order_shopState.setText("审核不通过");
                break;

            case Constant.ORDER_STATE_RETURN_REFUND_SUCCESS:
                viewHolder.order_shopState.setText("已退款");
                break;

            default:
                viewHolder.order_shopState.setText("");
                break;
        }

//			// 退货申请
//			if (detailList.get(position).getOrdStat()
//					.equals(Constant.ORDER_STATE_RETURN_GOODS_APPLY)) {
//				viewHolder.order_shopState.setText("退货申请");
//			}
//			//待退货
//			if (detailList.get(position).getOrdStat()
//					.equals(Constant.ORDER_STATE_RETURN_GOODS_WAIT)) {
//				viewHolder.order_shopState.setText("待退货");
//			}
//			// 退货已完成
//			if (detailList.get(position).getOrdStat()
//					.equals(Constant.ORDER_STATE_RETURN_GOODS_WAREHOUSEING)) {
//				viewHolder.order_shopState.setText("退款待审核");
//			}
//			// 退货已退款
//			if (detailList.get(position).getOrdStat()
//					.equals(Constant.ORDER_STATE_RETURN_GOODS_REFUND)) {
//				viewHolder.order_shopState.setText("退款完成");
//			}
//			// 配送中
//			if (detailList.get(position).getOrdStat()
//					.equals(Constant.ORDER_STATE_DISTR)) {
//				viewHolder.order_shopState.setText("配送中");
//			}

        // shop 价格 //订单金额
//        if (!TextUtils.isEmpty(detailList.get(position).ordCash)) {
        viewHolder.order_totalPrice.setText("￥" + detailList.get(position).ordCash);
//        } else {
//            viewHolder.order_totalPrice.setText("");
//        }
        // shop 数量
//        if (!TextUtils.isEmpty(detailList.get(position).groupBuyNum)) {
        viewHolder.order_totalNum.setText("X" + detailList.get(position).groupBuyNum);
//        } else {
//            viewHolder.order_totalNum.setText("");
//        }

        // shop 分类
        // if (!TextUtils.isEmpty(detailList.get(position).getGroupBuyNum())) {
        // viewHolder.order_color.setText(detailList.get(position).getGroupBuyNum());
        // }
    }

//    public static HashMap<Integer, Boolean> getIsSelected() {
//        return isSelected;
//    }
//
//    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
//        MyPurchasedetailListAdapter.isSelected = isSelected;
//    }

    private class ViewHolder {
        public TextView order_shopName;
        public ImageView order_image;
        public TextView order_shopState;
        public TextView order_title;
        public TextView order_totalPrice;
        public TextView order_totalNum;
        //        public TextView order_color;
        public TextView tv_buy_time;
//        public CheckBox my_purchase_checkbox;
    }

}
